package xin.yangshuai.jvm01.gc;

import java.util.HashSet;
import java.util.Objects;

/**
 * Hello
 *
 * @author shuai
 * @date 2022/1/9
 */
public class Hello {

//    private int[] array = new int[1024];

    private String name;

    private int age;

    public Hello() {
    }

    public Hello(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Hello hello = (Hello) o;
        return age == hello.age &&
                Objects.equals(name, hello.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }

    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        System.out.println("调用了finalize");
    }

    public static void main(String[] args) {
//        List<Hello> list = new ArrayList<>();
//        new Hello();
//        System.gc();
//        for (int i = 0; i < 10000; i++) {
////            list.add(new Hello());
//            new Hello();
//        }

        HashSet<Object> set = new HashSet<>();
        Hello hello1 = new Hello("123", 123);
        Hello hello2 = new Hello("456", 456);
        System.out.println(hello1.hashCode());
        System.out.println(hello2.hashCode());
        set.add(hello1);
        set.add(hello2);
        hello1.name = "qwe";
        hello1.age = 678;
        System.out.println(hello1.hashCode());
        set.remove(hello1);
//        boolean contains = set.contains(hello1);
//        System.out.println(contains);
        System.out.println(set);
    }

    @Override
    public String toString() {
        return "Hello{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
